package com.group.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.entity.ProjectInside;
import com.group.entity.ProjectOutside;
import com.group.entity.Talents;
import com.group.mapper.ProjectOutsideMapper;
import com.group.service.ProjectOutsideService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group.vo.DataVO;
import com.group.vo.ProjectVO;
import com.group.vo.TalentsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 朋心合力
 * @since 2021-08-09
 */
@Service
public class ProjectOutsideServiceImpl extends ServiceImpl<ProjectOutsideMapper, ProjectOutside> implements ProjectOutsideService {

    @Autowired
    ProjectOutsideMapper mapper;



    @Override
    public List<ProjectOutside> find(String tag1, String tag2) {
        return mapper.find(tag1, tag2);
    }

    public Page<ProjectOutside> getPages(Integer currentPage, Integer pageSize, List list) {
        Page page = new Page();
        int size = list.size();

        if (pageSize > size) {
            pageSize = size;
        }

        // 求出最大页数，防止currentPage越界
        int maxPage = size % pageSize == 0 ? size / pageSize : size / pageSize + 1;

        if (currentPage > maxPage) {
            currentPage = maxPage;
        }

        // 当前页第一条数据的下标
        int curIdx = currentPage > 1 ? (currentPage - 1) * pageSize : 0;

        List pageList = new ArrayList();

        // 将当前页的数据放进pageList
        for (int i = 0; i < pageSize && curIdx + i < size; i++) {
            pageList.add(list.get(curIdx + i));
        }

        page.setCurrent(currentPage).setSize(pageSize).setTotal(list.size()).setRecords(pageList);
        return page;
    }

    @Override
    public DataVO<ProjectVO> findData(Integer page, Integer limit) {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");
        //分页
        IPage<ProjectOutside> iPage = new Page<>(page,limit);
        IPage<ProjectOutside> projectOutsideIPage = mapper.selectPage(iPage, null);

        dataVO.setCount(projectOutsideIPage.getTotal());
        List<ProjectOutside> projectOutsideList = projectOutsideIPage.getRecords();
        List<ProjectVO> projectVOList = new ArrayList<>();
        for (ProjectOutside projectOutside : projectOutsideList) {
            ProjectVO projectVO = new ProjectVO();
            BeanUtils.copyProperties(projectOutside, projectVO);
            projectVOList.add(projectVO);
        }
        dataVO.setData(projectVOList);
        return dataVO;

    }

    @Override
    public DataVO<ProjectVO> findData2(Integer page, Integer limit, String zone) {
        DataVO dataVO = new DataVO();
        List<ProjectOutside> projectOutsideList = null;
        dataVO.setCode(0);
        dataVO.setMsg("");
        IPage<ProjectOutside> iPage = new Page<>(page, limit);
        QueryWrapper wrapper = new QueryWrapper();
        if (zone.equals("全部技术领域") || zone.equals("")) {
            IPage<ProjectOutside> projectOutsideIPage = mapper.selectPage(iPage, null);
            dataVO.setCount(projectOutsideIPage.getTotal());
            projectOutsideList = projectOutsideIPage.getRecords();
        } else {
            wrapper.like("projectTag", zone);
            IPage<ProjectOutside> projectOutsideIPage = mapper.selectPage(iPage, wrapper);
            dataVO.setCount(projectOutsideIPage.getTotal());
            projectOutsideList = projectOutsideIPage.getRecords();
        }
        List<ProjectVO> projectVOList = new ArrayList<>();
        for (ProjectOutside projectOutside : projectOutsideList) {
            ProjectVO projectVO = new ProjectVO();
            BeanUtils.copyProperties(projectOutside, projectVO);
            projectVOList.add(projectVO);
        }
        dataVO.setData(projectVOList);
        return dataVO;
    }

    //获取有在广场项目列表中的各个项目的id
    public List<ProjectOutside> getProjectId(){
        return  mapper.selectList(null);
    }

    //往广场项目表中添加一行信息并返回其项目编号
    public int addInProjectOutside(ProjectInside project,Integer currentUserId){
        ProjectOutside proO = new ProjectOutside();
        proO.setProjectName(project.getProjectName());
        proO.setMasterId(currentUserId);
        proO.setProjectDescription(project.getDescription());
        proO.setProjectTag(project.getPhotoName());
        mapper.insert(proO);
        QueryWrapper<ProjectOutside> wrapper = new QueryWrapper<>();
        wrapper.select("projectId")
                .eq("projectName",project.getProjectName())
                .eq("projectDescription",project.getDescription())
                .eq("projectTag",project.getPhotoName());
        return mapper.selectOne(wrapper).getProjectId();
    }

}
